package com.lepu.app.fun.chat.controller.base;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;

import com.app.application.MyApplication;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.BufferProgressDialog;
import com.core.lib.widget.SwipeBackLayout;
import com.eyzhs.app.R;


/**
 * Created by chuanyhu on 2014/10/8.
 */
public class BaseFragmentActivityChat extends BaseFragmentActivity {

    private AbsBaseFragment currentFragment;
    //	protected Logger logger = LoggerFactory
//			.getLogger(BaseFragmentActivity.class);
    private final String TAG = BaseFragmentActivityChat.this.getClass().getSimpleName();

    public BaseFragmentActivityChat() {
        ActivityManager activityManager = (ActivityManager) MyApplication
                .getInstance().getSystemService(Context.ACTIVITY_SERVICE);
        int memSize = activityManager.getMemoryClass();
        LogUtilBase.LogD(TAG, "BaseFragmentActivity getMemoryClass" + memSize);
    }

    @SuppressLint({"InflateParams", "ResourceAsColor"})
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onPause() {
        super.onPause();
        MyApplication.getInstance().setCurrentActivity(null);
    }

    @Override
    public void onResume() {
        super.onResume();
        MyApplication.getInstance().setCurrentActivity(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        AppManager.getAppManager().finishActivity(this);
    }

    public void setCurrentFragment(AbsBaseFragment currentFragment) {
        this.currentFragment = currentFragment;
    }

    public AbsBaseFragment getCurrentFragment() {
        return currentFragment;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        LogUtilBase.LogD(TAG, "[Focus] onWindowFocusChanged: hasFocus=" + hasFocus
                + " getSimpleName:" + this.getClass().getSimpleName());

        // Dont care if CommonActivity lost focus!
        if (!hasFocus
                && this.getClass().getSimpleName().equals("CommonActivity"))
            return;

        // // TODO: 2015/10/13
        // MyApplication.getInstance().setHasFocus(hasFocus);
        // MyApplication.getInstance().setHidden(!hasFocus);
        // logger.debug("[Focus] onWindowFocusChanged: Update  isHidden=" +
        // MyApplication.getInstance().isHidden() + " getSimpleName:" +
        // this.getClass().getSimpleName());
    }

    @SuppressLint("NewApi")
    @Override
    // TODO Change foe API 14 or above
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        // logger.debug("[Performance] onTrimMemory:" + level);
        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            LogUtilBase.LogD(TAG, "[Focus] onTrimMemory: TRIM_MEMORY_UI_HIDDEN. hidden=true");
            // MyApplication.getInstance().setHidden(true);
            // // TODO: 2015/10/13 进行app 优化时候 使用
            // logger.debug("[Focus] onTrimMemory: AppContext.isHidden=" +
            // AppContext.getInstance().isHidden());
            // if (currentFragment!=null){
            // logger.debug("[Performance] currentFragment: clearData");
            // currentFragment.clearData();
            // }
        }
        if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_RUNNING_MODERATE");
        if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_RUNNING_LOW ");
        if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)

            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_RUNNING_CRITICAL");
        if (level == ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_BACKGROUND ");
        if (level == ComponentCallbacks2.TRIM_MEMORY_MODERATE)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_MODERATE");
        if (level == ComponentCallbacks2.TRIM_MEMORY_COMPLETE)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_COMPLETE");
        if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)
            LogUtilBase.LogD(TAG, "[Performance] onTrimMemory: TRIM_MEMORY_RUNNING_CRITICAL");

    }

    @Override
    // TODO same as TRIM_MEMORY_COMPLETE
    public void onLowMemory() {
        super.onLowMemory();
        LogUtilBase.LogD(TAG, "[Performance] onLowMemory");
    }
}
